package com.kehutong.help.service;

import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.mvc.webservice.WebService;
import org.coraframework.orm.jdbc.JdbcSession;
import com.kehutong.help.entity.HelpClassify;

@WebService("/navigation/classify/service")
public class HelpClassifyService {

    @Inject
    private JdbcSession jdbcSession;

    public Object listPage(JSONObject jsonObject) {

        return Result.success(jdbcSession.findPage(HelpClassify.class)
                                         .like(jsonObject, "name")
                                         .eq("deleted", false)
                                         .order("enable", "desc")
                                         .order("sort", "asc")
                                         .page(jsonObject)
                                         .exe());
    }

    public Object list(JSONObject jsonObject) {

        return Result.success(jdbcSession.findArray(HelpClassify.class)
                                         .eq(jsonObject, "name")
                                         .eq("deleted", false)
                                         .order("sort", "asc")
                                         .exe());
    }

    public Object save(HelpClassify classify) {
        jdbcSession.insert(classify);

        return Result.success();
    }


    public Object update(HelpClassify classify) {
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object delete(HelpClassify classify) {
        if (classify.getCount() > 0) {
            return Result.error(0, "请先删除引用部分");
        }

        classify.setDeleted(true);
        jdbcSession.updateById(classify);

        return Result.success();
    }

    public Object updateSort(HelpClassify classify, Integer sort) {
        classify.setSort(sort);
        jdbcSession.updateById(classify);

        return Result.success();
    }

}
